package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.QueryInfo;
import com.ygqh.baby.ao.ResultSet;
import com.ygqh.baby.mapper.YgAddedUsersMapper;
import com.ygqh.baby.po.YgAddedUsers;
import com.ygqh.baby.po.YgAddedUsersExample;
import com.ygqh.baby.service.YgAddedUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Service
public class YgAddedUsersServiceImpl implements YgAddedUsersService {

    @Autowired
    private YgAddedUsersMapper ygAddedUsersMapper;

    @Override
    public List<YgAddedUsers> find() {
        YgAddedUsersExample example = new YgAddedUsersExample();
        return ygAddedUsersMapper.selectByExample(example);
    }

    @Override
    public int findCount() {
        YgAddedUsersExample example = new YgAddedUsersExample();
        return ygAddedUsersMapper.countByExample(example);
    }

    @Override
    public YgAddedUsers findById(Long id) {
        return ygAddedUsersMapper.selectByPrimaryKey(id);
    }


    @Override
    public int save(YgAddedUsers ygAddedUsers) {
        return ygAddedUsersMapper.insertSelective(ygAddedUsers);
    }

    @Override
    public int update(YgAddedUsers ygAddedUsers) {
        return ygAddedUsersMapper.updateByPrimaryKeySelective(ygAddedUsers);
    }

    @Override
    public ResultSet<YgAddedUsers> search(QueryInfo queryInfo, String q, Date createTime, Date endTime) {
        List<YgAddedUsers> list = ygAddedUsersMapper.selectSuper(queryInfo, q,createTime,endTime);
        int count = ygAddedUsersMapper.countSuper(q,createTime,endTime);
        ResultSet<YgAddedUsers> resultSet = new ResultSet<YgAddedUsers>(count, list);
        return resultSet;
    }

    @Override
    public int deleteSoft(Long[] ids) {
        Assert.notEmpty(ids);
        YgAddedUsersExample example = new YgAddedUsersExample();
        example.createCriteria().andIdIn(Arrays.asList(ids));
        YgAddedUsers record = new YgAddedUsers();
        return ygAddedUsersMapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteSoft(Long id) {
        return this.deleteSoft(new Long[]{id});
    }

    @Override
    public int saveOrUpdate(YgAddedUsers ygAddedUsers) {
        int r = 0;
        YgAddedUsersExample example = new YgAddedUsersExample();
        example.createCriteria().andCreateTimeEqualTo(ygAddedUsers.getCreateTime());
        List<YgAddedUsers> usersList = ygAddedUsersMapper.selectByExample(example);

        if (usersList.isEmpty()) {
            this.save(ygAddedUsers);
        } else {
            YgAddedUsers users = usersList.get(0);
            ygAddedUsers.setId(users.getId());
            this.update(ygAddedUsers);
        }
        return r;
    }

}

